package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.system.domain.Quality;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Equipment;
import com.ruoyi.system.service.IEquipmentService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 设备数据Controller
 *
 * @author ruoyi
 * @date 2024-04-20
 */
@RestController
@RequestMapping("/system/equipment")
public class EquipmentController extends BaseController
{
    @Autowired
    private IEquipmentService equipmentService;

    /**
     * 查询设备数据列表
     */
    @PreAuthorize("@ss.hasPermi('system:equipment:list')")
    @GetMapping("/list")
    public TableDataInfo list(Equipment equipment)
    {
        startPage();
        List<Equipment> list = equipmentService.selectEquipmentList(equipment);
        return getDataTable(list);
    }


    @Anonymous
    @GetMapping("/charts")
    public List<Equipment> charts(Equipment equipment)
    {
        return equipmentService.selectEquipmentList(equipment);
    }
    /**
     * 导出设备数据列表
     */
    @PreAuthorize("@ss.hasPermi('system:equipment:export')")
    @Log(title = "设备数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Equipment equipment)
    {
        List<Equipment> list = equipmentService.selectEquipmentList(equipment);
        ExcelUtil<Equipment> util = new ExcelUtil<Equipment>(Equipment.class);
        util.exportExcel(response, list, "设备数据数据");
    }

    @Log(title = "设备管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:equipment:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<Equipment> util = new ExcelUtil<Equipment>(Equipment.class);
        List<Equipment> equipmentList = util.importExcel(file.getInputStream());
        String message = equipmentService.importEquipment(equipmentList, updateSupport);
        return success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<Equipment> util = new ExcelUtil<Equipment>(Equipment.class);
        util.importTemplateExcel(response, "设备数据");
    }


    @Anonymous
    @GetMapping("frontList")
    public AjaxResult frontList(){
        return success(equipmentService.selectEquipmentList(new Equipment()));
    }

    /**
     * 获取设备数据详细信息
     */
    @PreAuthorize("@ss.hasPermi('syst em:equipment:query')")
    @GetMapping(value = "/{eId}")
    public AjaxResult getInfo(@PathVariable("eId") Long eId)
    {
        return success(equipmentService.selectEquipmentByEId(eId));
    }

    /**
     * 新增设备数据
     */
    @PreAuthorize("@ss.hasPermi('system:equipment:add')")
    @Log(title = "设备数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Equipment equipment)
    {
        return toAjax(equipmentService.insertEquipment(equipment));
    }

    /**
     * 修改设备数据
     */
    @PreAuthorize("@ss.hasPermi('system:equipment:edit')")
    @Log(title = "设备数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Equipment equipment)
    {
        return toAjax(equipmentService.updateEquipment(equipment));
    }

    /**
     * 删除设备数据
     */
    @PreAuthorize("@ss.hasPermi('system:equipment:remove')")
    @Log(title = "设备数据", businessType = BusinessType.DELETE)
    @DeleteMapping("/{eIds}")
    public AjaxResult remove(@PathVariable Long[] eIds)
    {
        return toAjax(equipmentService.deleteEquipmentByEIds(eIds));
    }
}
